package com.ldoublem.loadingviewlib.view;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import com.ldoublem.loadingviewlib.view.base.LVBase;


public class LVCircularRing extends LVBase implements Component.EstimateSizeListener, Component.DrawTask {

    private Paint mPaint;
    private Paint mPaintPro;

    private float mWidth = 0f;
    private float mPadding = 0f;
    private float startAngle = 0f;
    RectFloat rectF = new RectFloat();

    public LVCircularRing(Context context) {
        super(context);
    }

    public LVCircularRing(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    public LVCircularRing(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }







    private void initPaint() {
        addDrawTask(this);
        setEstimateSizeListener(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(8);

        mPaintPro=new Paint();
        mPaintPro.setAntiAlias(true);
        mPaintPro.setStyle(Paint.Style.STROKE_STYLE);
        mPaintPro.setColor(new Color(Color.argb(100, 255, 255, 255)));
        mPaintPro.setStrokeWidth(8);

    }


    public void setViewColor(int color)
    {
        mPaintPro.setColor(new Color(color));
        invalidate();
    }

    public void setBarColor(int color)
    {
        mPaint.setColor(new Color(color));
        invalidate();
    }




    @Override
    protected void InitPaint() {
        initPaint();
    }

    @Override
    protected void OnAnimationUpdate(AnimatorValue valueAnimator, float v) {
        float value = v;
        startAngle = 360 * value;

        invalidate();
    }

    @Override
    protected void OnAnimationRepeat(Animator animation) {

    }





    @Override
    protected int OnStopAnim() {
        return 0;
    }
    @Override
    protected int SetAnimRepeatMode() {
        return RESTART;
    }
    @Override
    protected void AinmIsRunning() {

    }
    @Override
    protected int SetAnimRepeatCount() {
        return INFINITE;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2 - mPadding, mPaintPro);
        rectF = new RectFloat(mPadding, mPadding, mWidth - mPadding, mWidth - mPadding);
        canvas.drawArc(rectF, new Arc(startAngle, 100
                , false), mPaint);//第四个参数是否显示半径
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        if (EstimateSpec.getSize(i) > getHeight())
            mWidth = EstimateSpec.getSize(i1);
        else
            mWidth = EstimateSpec.getSize(i);
        mPadding = 5;
        return false;
    }
}
